const permissionService = require('@/services/permission.service')
const JsonResult = require('@/utils/httpResponse.util')

class PermissionController {
  async findRoleList (req, response) {
    const { name, status } = req.query
    try {
      const data = await permissionService.findRoles(name, status)
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  /**
   * 角色创建
   * @param {*} req
   * @param {*} response
   * @returns
   */
  async createRole (req, response) {
    const params = req.body
    try {
      const data = await permissionService.createRole(params)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '新增成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '新增失败' })
    }
  }

  /**
   * 角色更新
   * @param {*} req
   * @param {*} response
   * @returns
   */
  async updateRole (req, response) {
    const params = req.body
    try {
      const data = await permissionService.updateRole(params)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '更新成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新失败' })
    }
  }

  /**
   * 角色删除
   * @param {*} req
   * @param {*} response
   * @returns
   */
  async deleteRole (req, response) {
    const id = req.query.id
    try {
      const data = await permissionService.deleteRole(id)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }

  /**
   * 角色权限分配
   * @param {*} req
   * @param {*} response
   */
  async roleAllocationPermission (req, response) {
    const { roleId, permissionIds } = req.body
    try {
      const data = await permissionService.roleAllocationPermission(roleId, permissionIds)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '角色分配权限成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '角色权限分配失败' })
    }
  }

  async findRolePermissionIds (req, response) {
    const roleId = req.query.roleId
    try {
      const data = await permissionService.findRolePermissionIds(roleId)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }
}

module.exports = new PermissionController()
